package com.sunline.poc.oozie.test.demo;

import org.apache.hadoop.security.UserGroupInformation;
import org.apache.oozie.client.*;
import org.apache.hadoop.conf.Configuration;

import java.io.IOException;
import java.security.PrivilegedAction;
import java.util.List;
import java.util.Properties;

public class JavaWorkflowDemo {
    private static String oozieURL = "http://n1:11000/oozie";
    public static void main(String[] args) throws IOException {
        System.setProperty("sun.security.krb5.debug", "true");
        System.setProperty("java.security.krb5.conf", "D:\\Temp\\krb5.conf");
        Configuration conf = new Configuration();
        conf.set("hadoop.security.authentication","kerberos");
        conf.set("oozie.security.authentication","kerberos");
        UserGroupInformation.setConfiguration(conf);
        UserGroupInformation.loginUserFromKeytab("clientA@DC.COM","D:\\Temp\\clientA.keytab");
        UserGroupInformation ugi = UserGroupInformation.loginUserFromKeytabAndReturnUGI("clientA@DC.COM", "D:\\Temp\\clientA.keytab");
        ugi.doAs(new PrivilegedAction<Object>() {
            @Override
            public Object run() {
                subJob();
                return null;
            }
        });

        System.out.println("hello,world!");
    }


    public  static  void subJob(){
        AuthOozieClient oozieClient = new AuthOozieClient(oozieURL, AuthOozieClient.AuthType.KERBEROS.name());
        oozieClient.setDebugMode(1);

        try {
            Properties properties = oozieClient.createConfiguration();
            properties.put("oozie.use.system.libpath", "True");
            properties.put("nameNode", "hdfs://n1:8020");
            properties.put("jobTracker", "n1:8032");
            properties.put("mainClass", "org.apache.hadoop.examples.QuasiMonteCarlo");
            properties.put("arg1", "10");
            properties.put("arg2", "10");
            properties.put("javaOpts", "-Xmx200m");
            properties.put("oozie.libpath", "${nameNode}/clientA/jars/");
            properties.put("oozie.wf.application.path", "${nameNode}/user/clientA/oozie/javaaction");
            properties.put("user.name","clientA");
            //运行workflow
            String jobid = oozieClient.run(properties);
            System.out.println("jobid="+jobid);
            //等待10s
            Thread.sleep(10000l);
            //根据workflow id获取作业运行情况
            WorkflowJob workflowJob = oozieClient.getJobInfo(jobid);
            //获取作业日志
            System.out.println(oozieClient.getJobLog(jobid));
            //获取workflow中所有ACTION
            List<WorkflowAction> list = workflowJob.getActions();
            for (WorkflowAction action : list) {
                //输出每个Action的 Appid 即Yarn的Application ID
                System.out.println(action.getExternalId());
            }
            //杀掉作业
//            oozieClient.kill(jobid);
        } catch (OozieClientException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
